package com.example.product.biz.controller;

import com.alibaba.fastjson.JSON;
import com.example.common.constant.MessageConstant;
import com.example.common.domain.PageResult;
import com.example.common.domain.Result;
import com.example.product.biz.domain.dto.ProductSearchDTO;
import com.example.product.biz.domain.po.ProductDoc;
import com.example.product.biz.domain.vo.ProductDetailVO;
import com.example.product.biz.domain.vo.RecommendProductVO;
import com.example.product.biz.service.ProductService;
import com.example.product.domain.dto.ItemDTO;
import com.example.product.domain.vo.ProductVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Tag(name = "商品接口相关")
@Slf4j
@RestController
@RequestMapping("/product")
@RequiredArgsConstructor
public class ProductController {
    private final ProductService productService;
    private final RestHighLevelClient client;

    @Operation(summary = "添加商品信息")
    @PostMapping
    public Result addProduct() {
        log.info("添加商品信息");
        return Result.success();
    }

    /**
     * 商品页展示
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Operation(summary = "商品页展示信息")
    @GetMapping
    public Result<PageResult<RecommendProductVO>> getProduct(@RequestParam(defaultValue = "1") String page,
                                                             @RequestParam(defaultValue = "10") String pageSize) {
        Integer pageNum = Integer.valueOf(page);
        Integer size = Integer.valueOf(pageSize);
        log.info("获取商品信息:{}页,{}条", page, pageSize);
        PageResult<RecommendProductVO> pageResult = productService.getProduct(pageNum, size);
        return Result.success(pageResult);
    }



    @Operation(summary = "商品搜索信息")
    @GetMapping("/search")
    public Result<PageResult<RecommendProductVO>> searchProduct(ProductSearchDTO searchDTO) {
        log.info("搜索商品信息:{}", searchDTO);
        PageResult<RecommendProductVO> pageResult = productService.searchProduct(searchDTO);
        return Result.success(pageResult);
    }
    @Operation(summary = "商品详情信息")
    @GetMapping("/{id}")
    public Result<ProductDetailVO> getProductById(@PathVariable Integer id) {
        log.info("获取商品信息:{}", id);
        ProductDetailVO productDetailVO = productService.getProductById(id);
        return Result.success(productDetailVO);
    }

    @Operation(summary = "购物车信息")
    @GetMapping("/info")
    public Result<Map<Long, ProductVO>> getProductToMap(@RequestParam List<Long> ids) {
        log.info("判断商品是否有效:{}", ids);
        Map<Long, ProductVO> productVOMap = productService.getProductToMap(ids);
        return Result.success(productVOMap);
    }

    /* @Operation(summary = "扣减库存")
     @PutMapping("/stock/deduct")
     public Result deductStock(@RequestBody List<ItemDTO> itemDTO) {
         log.info("扣减库存:{}", itemDTO);
         try {
             productService.deductStock(itemDTO);
         } catch (Exception e) {
           return Result.error(e.getMessage());
         }
         return Result.success();
     }*/
    @Operation(summary = "扣减库存")
    @PutMapping("/stock/deduct")
    public Result deductStock(@RequestBody List<ItemDTO> itemDTO) {
        log.info("扣减库存:{}", itemDTO);
        productService.deductStock(itemDTO);
        return Result.success();
    }

    /**
     * 恢复库存
     *
     * @param productId
     * @param num
     * @return
     */
    @Operation(summary = "恢复库存")
    @PutMapping("/restoreStock")
    public Result restoreStock(Integer skuId, Integer num) {
        log.info("恢复库存:{},数量:{}", skuId, num);
        productService.restoreStock(skuId, num);
        return Result.success();
    }

    /**
     * 同步所有商品信息到Elasticsearch
     *
     * @return
     */
    @Operation(summary = "同步所有商品信息到Elasticsearch")
    @PutMapping("/syncAllProductsToElasticsearch")
    public Result syncAllProductsToElasticsearch() {
        log.info("同步所有商品信息到Elasticsearch");
        productService.syncAllProductsToElasticsearch();
        return Result.success();
    }

}
